package com.cqie.tes.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqie.tes.comm.TR;
import com.cqie.tes.comm.service.impl.BaseServiceImpl;
import com.cqie.tes.config.MyUserDetile;
import com.cqie.tes.domain.RoleMenu;
import com.cqie.tes.domain.SysMenu;
import com.cqie.tes.domain.SysRole;
import com.cqie.tes.domain.vo.MenuVo;
import com.cqie.tes.mapper.RoleMenuMapper;
import com.cqie.tes.mapper.SysMenuMapper;
import com.cqie.tes.mapper.SysRoleMapper;
import com.cqie.tes.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MenuServiceImpl extends BaseServiceImpl<SysMenuMapper, SysMenu> implements MenuService {

    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    SysMenuMapper sysMenuMapper;
    @Autowired
    RoleMenuMapper roleMenuMapper;



    @Override
    public TR menu() {

        // 获取身份
        MyUserDetile principal = (MyUserDetile) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //

        // 根据用户的角色拿到菜单

        List<String> roleNames = principal.getAuthorities()
            .stream()
            .map(GrantedAuthority::getAuthority)
            .collect(Collectors.toList());

        if (roleNames.isEmpty()){
            return TR.ok().data(new ArrayList());
        }

        List<Long> roleIds = sysRoleMapper.selectList(new QueryWrapper<SysRole>()
                .lambda()
                .in(SysRole::getName, roleNames))
                .stream()
                .map(SysRole::getId)
                .collect(Collectors.toList());

        if (roleIds.isEmpty()){
            return TR.ok().data(new ArrayList());
        }
        // 根据rolemenu得到menu
        List<Long> menuIds = roleMenuMapper.selectList(new QueryWrapper<RoleMenu>()
                .lambda()
                .in(RoleMenu::getRoleId, roleIds))
                .stream()
                .map(RoleMenu::getMenuId)
                .collect(Collectors.toList());

        if (menuIds.isEmpty()){
            return TR.ok().data(new ArrayList());
        }

        // 查询这个用户的父级菜单Ids
        List<SysMenu> fmenus = sysMenuMapper.selectList(new QueryWrapper<SysMenu>()
                .lambda()
                .eq(SysMenu::getParentId, 0)
                .eq(SysMenu::getMark,1)
                .in(SysMenu::getId, menuIds)
                .orderByAsc(SysMenu::getSortCode));

        List<MenuVo> vos = new ArrayList<>();
        fmenus.forEach(entity->{
            MenuVo vo = new MenuVo();
            vo.setName(entity.getId().toString());
            vo.setIcon(entity.getImgUrl());
            vo.setJump(entity.getAppUrl());
            vo.setTitle(entity.getName());

            // 一级指标列表
            List<Long> fmenuIds = sysMenuMapper.selectList(new QueryWrapper<SysMenu>()
                    .lambda()
                    .eq(SysMenu::getParentId,entity.getId())
                    .eq(SysMenu::getMark,1)
                    .in(SysMenu::getId, menuIds)
                    .orderByAsc(SysMenu::getSortCode))
                    .stream()
                    .map(SysMenu::getId)
                    .collect(Collectors.toList());

            getTree(fmenuIds,vo,menuIds);

            vos.add(vo);

        });


        return TR.ok().data(vos);
    }
    public void getTree(List<Long> fatherIds,MenuVo data,List<Long> menuIds){
        if (fatherIds.isEmpty()){
            data.setList(new ArrayList<>());
            return;
        }
        // 查出这个所有ids包含列表
        List<SysMenu> entityList = sysMenuMapper.selectList(new QueryWrapper<SysMenu>()
                .lambda()
                .in(SysMenu::getId, fatherIds)
                .orderByAsc(SysMenu::getSortCode));

        List<MenuVo> vos = new ArrayList<>();
        entityList.forEach(entity->{
            MenuVo vo = new MenuVo();
            vo.setName(entity.getId().toString());
            vo.setIcon(entity.getImgUrl());
            vo.setJump(entity.getAppUrl());
            vo.setTitle(entity.getName());

            List<Long> ids = sysMenuMapper.selectList(new QueryWrapper<SysMenu>()
                    .lambda()
                    .eq(SysMenu::getParentId,entity.getId())
                    .eq(SysMenu::getMark,1)
                    .in(SysMenu::getId, menuIds)
                    .orderByAsc(SysMenu::getSortCode))
                    .stream()
                    .map(SysMenu::getId)
                    .collect(Collectors.toList());

            getTree(ids,vo,menuIds);

            vos.add(vo);
        });
        data.setList(vos);

    }

}
